#include "stdafx.h"
#include <cctype>
#include <fstream>
#include <cassert>

#include "mc_driver.hpp"
#include "Node.h"

// wamckee 2013 APR 2 - Instead of testing for == FAIL below,
// we test for != ACCEPT (in which ACCEPT has a value of zero).
// When compiling on a MSVC 2010 platform using cygwin's Flex and Bison,
// we note that the return value from parse() is not -1 but rather +1.
// It is more likely that on different platforms the return value of zero
// will be used consistantly to indicate an accept state. Whereas, the
// value of the abort or fail state will be more likely not to be a specific
// value but rather a value that is not equal to zero.


MC::MC_Driver::~MC_Driver(){ 
   delete(scanner);  // wamckee 2013 APR 2
   delete(parser);   // wamckee 2013 APR 2
}

NODE* MC::MC_Driver::parse( const char *queryLength ){

   NODE * node = NULL;
   delete(scanner); // wamckee 2013 APR 2



   char str[256];

   memcpy(str,queryLength,strlen(queryLength)+1);

   std::ifstream is(str);     


   scanner = new MC::MC_Scanner( &is );
   /* check to see if its initialized */
   assert( scanner != nullptr );
   
   delete(parser); // wamckee 2013 APR 2
   parser = new MC::MC_Parser( (*scanner) /* scanner */, 
                               (*this) /* driver */ );
   assert( parser != nullptr );
   
   if(parser->parse() != 0) // wamckee 2013 APR 2
   {
      std::cerr << "Parse failed!!\n";
   }
    return 0;
}


std::ostream& MC::MC_Driver::print(std::ostream &stream){
    stream << "YOU ARE RIGHT"<<std::endl;
   return(stream);
}
